package com.jc.dao.toy.impl;

import com.jc.base.Assert;
import com.jc.base.ComponentException;
import com.jc.base.page.Page;
import com.jc.base.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;
import com.jc.dao.toy.ToyAberdeenCatgoryDao;
import com.jc.domain.toy.ToyAberdeenCatgory;
import com.jc.domain.toy.ToyAberdeenCatgoryConditions;
import com.jc.mapper.toy.ToyAberdeenCatgoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.jc.repository.generator.utils.GenerateMybaitsUtils;
import com.jc.repository.generator.utils.Condition;
import com.jc.repository.generator.utils.ConditionsExtend;
import com.jc.utils.CollectionUtils;

import java.util.List;
import java.util.ArrayList;

/**
 * @author
 * @create 2016-12-13 09:32:26
 * @description
 */

@Repository
public class ToyAberdeenCatgoryDaoImpl implements ToyAberdeenCatgoryDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(ToyAberdeenCatgoryDaoImpl.class);

    @Autowired
    private ToyAberdeenCatgoryMapper mapper;

    //this function automatic generated by application
    @Override
    public ToyAberdeenCatgory queryToyAberdeenCatgoryById(Long id) {

        Assert.notNull(id, "用于查询的id不能为空");
        return mapper.selectByPrimaryKey(id);
    }

    //this function automatic generated by application
    @Override
    public ResultData<List<ToyAberdeenCatgory>> queryToyAberdeenCatgorysByConditions(ToyAberdeenCatgory toyAberdeenCatgory, String orderByClause, Page page) {

        ResultData<List<ToyAberdeenCatgory>> res = new ResultData<>();
        if(null == toyAberdeenCatgory && LOGGER.isInfoEnabled()){
            LOGGER.info("用于查询的条件为空, 将查询toyAberdeenCatgory表的所有数据");
        }
        ToyAberdeenCatgoryConditions conditions = new ToyAberdeenCatgoryConditions();

        ToyAberdeenCatgoryConditions.Criteria criteria = conditions.or();

        GenerateMybaitsUtils.buildConditionsByReflect(criteria, toyAberdeenCatgory);

        if (null != page) {
            conditions.setPage(page);
            page.setTotalNum(mapper.countByExample(conditions));
            res.setPage(page);
        }

        if (null != orderByClause) {
            conditions.setOrderByClause(orderByClause);
        }

        res.setData(mapper.selectByExample(conditions));
        return res;
    }

    //this function automatic generated by application
    @Override
    public List<ToyAberdeenCatgory> queryToyAberdeenCatgorysByConditions(ToyAberdeenCatgory toyAberdeenCatgory, String orderByClause){

        if(null == toyAberdeenCatgory && LOGGER.isInfoEnabled()){
        	LOGGER.info("用于查询的条件为空, 将查询toyAberdeenCatgory表的所有数据");
        }
        ToyAberdeenCatgoryConditions conditions = new ToyAberdeenCatgoryConditions();

        ToyAberdeenCatgoryConditions.Criteria criteria = conditions.or();

        GenerateMybaitsUtils.buildConditionsByReflect(criteria, toyAberdeenCatgory);

        if (null != orderByClause) {
            conditions.setOrderByClause(orderByClause);
        }

        return mapper.selectByExample(conditions);
    }

    //this function automatic generated by application
    @Override
    public List<ToyAberdeenCatgory> queryToyAberdeenCatgorysByConditionEx(ToyAberdeenCatgory toyAberdeenCatgory, String orderByClause, Condition condition) {

        Assert.notNull(toyAberdeenCatgory, "用于查询的类,不能为空");
        ToyAberdeenCatgoryConditions conditions = new ToyAberdeenCatgoryConditions();

        ToyAberdeenCatgoryConditions.Criteria criteria = conditions.or();
        GenerateMybaitsUtils.buildConditionEx(criteria,toyAberdeenCatgory,condition);

        if (null != orderByClause) {
            conditions.setOrderByClause(orderByClause);
        }

        return mapper.selectByExample(conditions);

    }

    //this function automatic generated by application
    @Override
    public List<ToyAberdeenCatgory> queryToyAberdeenCatgorysByConditionExs(ConditionsExtend conditionsExtend, String orderByClause) {
        Assert.notNull(conditionsExtend, "用于查询的类,不能为空");
        ToyAberdeenCatgoryConditions conditions = new ToyAberdeenCatgoryConditions();

        ToyAberdeenCatgoryConditions.Criteria criteria = conditions.or();

        GenerateMybaitsUtils.buildConditionExs(criteria, conditionsExtend);

        if (null != orderByClause) {
            conditions.setOrderByClause(orderByClause);
        }

        return mapper.selectByExample(conditions);
    }

    //this function automatic generated by application
    @Override
    public ToyAberdeenCatgory saveOrInsert(ToyAberdeenCatgory toyAberdeenCatgory) {

        Assert.notNull(toyAberdeenCatgory, "用于保存的对象不能为空");

        GenerateMybaitsUtils.checkNotNull(toyAberdeenCatgory);

        if (null != toyAberdeenCatgory.getId()) {
            Assert.notNull(toyAberdeenCatgory.getId(), "用于更新的对象id不能为空");
            mapper.updateByPrimaryKeySelective(toyAberdeenCatgory);
        } else {
            mapper.insertSelective(toyAberdeenCatgory);
        }
        return toyAberdeenCatgory;

    }

    //this function automatic generated by application
    @Transactional
    @Override
    public List<ToyAberdeenCatgory> saveOrInsertList(List<ToyAberdeenCatgory> toyAberdeenCatgorys) {

        Assert.isTrue(CollectionUtils.isNotEmpty(toyAberdeenCatgorys), "添加的toyAberdeenCatgory集合禁止为空");
        ArrayList<ToyAberdeenCatgory> toyAberdeenCatgorysSaved = Lists.newArrayListWithCapacity(toyAberdeenCatgorys.size());

        for (ToyAberdeenCatgory toyAberdeenCatgory:toyAberdeenCatgorys) {
            toyAberdeenCatgorysSaved.add(saveOrInsert(toyAberdeenCatgory));
        }
        return toyAberdeenCatgorysSaved;
    }

    //this function automatic generated by application
    @Override
    public boolean deleteToyAberdeenCatgoryById(Long id) {

        Assert.notNull(id, "属性id不能为空");
        boolean result = false;
        try {
             result =  mapper.deleteByPrimaryKey(id) > 0;
        } catch (Exception e) {
            throw new ComponentException(ComponentException.ErrCode.系统异常,"插入异常");
        }
        return result;
    }

    //this function automatic generated by application
    @Override
    public void deleteByConditions(ToyAberdeenCatgory toyAberdeenCatgory) {
        Assert.notNull(toyAberdeenCatgory, "构造删除条件的类不能为空");

        ToyAberdeenCatgoryConditions conditions = new ToyAberdeenCatgoryConditions();

        ToyAberdeenCatgoryConditions.Criteria criteria = conditions.or();

        GenerateMybaitsUtils.buildConditionsByReflect(criteria,toyAberdeenCatgory);
        mapper.deleteByExample(conditions);

    }


}